package com.fancy.recommendation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fancy.recommendation.entity.Recommendation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 推荐结果Mapper
 *
 * @author fancy
 */
@Mapper
public interface RecommendationMapper extends BaseMapper<Recommendation> {

    /**
     * 获取用户有效推荐
     */
    @Select("SELECT * FROM t_recommendation WHERE user_id = #{userId} AND rec_type = #{recType} " +
            "AND status = 1 AND (expire_time IS NULL OR expire_time > NOW()) " +
            "ORDER BY score DESC LIMIT #{limit}")
    List<Recommendation> getValidRecommendations(@Param("userId") Long userId,
                                               @Param("recType") String recType,
                                               @Param("limit") Integer limit);

    /**
     * 清理过期推荐
     */
    int deleteExpiredRecommendations(@Param("expireTime") LocalDateTime expireTime);

    /**
     * 批量插入推荐结果
     */
    int batchInsert(@Param("recommendations") List<Recommendation> recommendations);

    /**
     * 获取用户推荐统计
     */
    List<java.util.Map<String, Object>> getUserRecommendationStats(@Param("userId") Long userId);

    /**
     * 根据算法类型获取推荐
     */
    List<Recommendation> getRecommendationsByAlgorithm(@Param("userId") Long userId,
                                                      @Param("algorithm") String algorithm,
                                                      @Param("limit") Integer limit);

    /**
     * 更新推荐状态
     */
    int updateRecommendationStatus(@Param("id") Long id, @Param("status") Integer status);
}
